package com.sdy.configcenter.web.controller;

import com.sdy.common.model.Response;
import com.sdy.common.utils.EncodeUtil;
import com.sdy.configcenter.biz.constants.RedisConstants;
import com.sdy.configcenter.biz.model.WxOaAccessToken;
import com.sdy.configcenter.biz.service.WxOaAccessTokenService;
import com.sdy.mvc.controller.BaseController;
import com.sdy.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzq
 * @since 2020-05-27
 */
@Slf4j
@RestController
@RequestMapping("/wxoa")
public class WxOaController extends BaseController {
    @Autowired
    private WxOaAccessTokenService wxOaAccessTokenService;
    @Autowired
    private RedisService redisService;

    @GetMapping("/accessToken")
    public Response<String> accessToken(String appid, String signature, Long timestamp, String nonce) {
        if (Math.abs(System.currentTimeMillis() - timestamp) > 1000 * 60 * 10) {
            return Response.error("时间戳不正确");
        }
        if (redisService.exists(RedisConstants.NONCE_VALID + timestamp.toString() + nonce)) {
            return Response.error("非法请求");
        }
        WxOaAccessToken wxOaAccessToken = wxOaAccessTokenService.getByAppid(appid);
        if (wxOaAccessToken == null) {
            return Response.error("AppId不存在");
        }
        redisService.set(RedisConstants.NONCE_VALID + timestamp.toString() + nonce, 1, 1000 * 60 * 15);
        String s = EncodeUtil.sha256(appid + "#" + wxOaAccessToken.getAppsecret() + "#" + timestamp + "#" + nonce);
        if (!s.equals(signature)) {
            return Response.error("签名失败");
        }
        return Response.success(wxOaAccessToken.getAccessToken());
    }

    @GetMapping("/jsApiTicket")
    public Response<String> jsApiTicket(String appid, String signature, Long timestamp, String nonce) {
        if (Math.abs(System.currentTimeMillis() - timestamp) > 1000 * 60 * 10) {
            return Response.error("时间戳不正确");
        }
        if (redisService.exists(RedisConstants.NONCE_VALID + timestamp.toString() + nonce)) {
            return Response.error("非法请求");
        }
        WxOaAccessToken wxOaAccessToken = wxOaAccessTokenService.getByAppid(appid);
        if (wxOaAccessToken == null) {
            return Response.error("AppId不存在");
        }
        String s = EncodeUtil.sha256(appid + "#" + wxOaAccessToken.getAppsecret() + "#" + timestamp + "#" + nonce);
        if (!s.equals(signature)) {
            return Response.error("签名失败");
        }
        redisService.set(RedisConstants.NONCE_VALID + timestamp.toString() + nonce, 1, 1000 * 60 * 15);
        return Response.success(wxOaAccessToken.getJsApiTicket());
    }

    public static void main(String[] args) {
        System.out.println(EncodeUtil.sha256("wxe2cc8190ee1a2d95#3008c6dea67410dfa04a2398aaf47108#1590566684295#2343r3"));
    }
}
